﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class NationalCup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.NationalCup_PageTitle;
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    Response.Redirect("Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            //if (ou.TeamId == "Null")
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            
            LanguageHiddenField.Value = ou.Language;

            string CountryId = null;
            string CorrectSeason = "0";
            string CorrectRound = "0";
            if (Request.QueryString["CountryId"] != null)
            {
                CountryId = Request.QueryString["CountryId"].ToString();                
            }
            if (CountryId == null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                CountryId = (GND.ReadNivdalIntData("@UserId", "Nivdal_GetCountryByUser", ou.UserId));
            }
            if (CountryId == null)
                Response.Redirect("~/Default.aspx");

            CountryIdHiddenField.Value = CountryId;

            if (Request.QueryString["CorrectSeason"] != null)
                CorrectSeason = Request.QueryString["CorrectSeason"].ToString();

            if (Request.QueryString["CorrectRound"] != null)
                CorrectRound = Request.QueryString["CorrectRound"].ToString();

            if (CorrectSeason == "0")
            {
                CorrectSeason = (GND.ReadNivdalIntData("@Country", "Nivdal_GetNationalCupCurrentSeason", CountryId));
                CorrectRound = (GND.ReadNivdalIntData("@Country", "@Season", "Nivdal_GetNationalCupCurrentRound", CountryId, CorrectSeason));
                //this.ErrorLabel.Text = CorrectSeason;
            }

            else if (CorrectRound == "0")
            {
                CorrectRound = (GND.ReadNivdalIntData("@Country", "@Season", "Nivdal_GetNationalCupCurrentRound", CountryId, CorrectSeason));
                //this.ErrorLabel.Text = CorrectRound;
            }

            if (!IsPostBack)
            {
                this.SqlDataSource1.SelectParameters.Add("Country", CountryId);
                this.SqlDataSource1.SelectParameters.Add("Season", CorrectSeason);
                this.SqlDataSource1.SelectParameters.Add("Round", CorrectRound);
                this.SqlDataSource1.SelectParameters.Add("Language", ou.Language);
                this.SqlDataSource2.SelectParameters.Add("Country", CountryId);
                this.SqlDataSource2.SelectParameters.Add("Season", CorrectSeason);
                this.SqlDataSource2.SelectParameters.Add("Round", CorrectRound);
                this.SqlDataSource2.SelectParameters.Add("Language", ou.Language);
                this.SeasonSqlDataSource.SelectParameters.Add("Country", CountryId);
                this.SeasonSqlDataSource.SelectParameters.Add("Language", ou.Language);

                this.NationalCupHistoryHyperLink.NavigateUrl = "NationalCupHistory.aspx?CountryId=" + CountryId;
            }

            
            if (GridView1.Rows.Count == 0)
            {
                AddToLiveAllPanel.Visible = false;
                MatchesTitleLabel.Visible = false;
            }
            else
            {
                AddToLiveAllPanel.Visible = false;
                MatchesTitleLabel.Visible = false;
            }
            if (ou.TeamId == null)
                AddToLiveAllPanel.Visible = false;
            

        }


        protected void SeasonDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string CountryId = CountryIdHiddenField.Value;

            if (this.SeasonDropDownList.SelectedValue == "-1")
            {
                this.ChooseRoundLabel.Visible = false;
                this.RoundDropDownList.Visible = false;
            }
            else
            {
                this.ChooseRoundLabel.Visible = true;
                this.RoundDropDownList.Visible = true;
            }


            this.SqlDataSource1.SelectParameters.Clear();
            this.SqlDataSource2.SelectParameters.Clear();
            this.RoundSqlDataSource.SelectParameters.Clear();
            this.SqlDataSource1.SelectParameters.Add("Country", CountryId);
            this.SqlDataSource1.SelectParameters.Add("Season", this.SeasonDropDownList.SelectedValue);
            this.SqlDataSource1.SelectParameters.Add("Language", LanguageHiddenField.Value);
            this.SqlDataSource2.SelectParameters.Add("Country", CountryId);
            this.SqlDataSource2.SelectParameters.Add("Season", this.SeasonDropDownList.SelectedValue);
            this.SqlDataSource2.SelectParameters.Add("Language", LanguageHiddenField.Value);
            this.RoundSqlDataSource.SelectParameters.Add("Country", CountryId);
            this.RoundSqlDataSource.SelectParameters.Add("Season", this.SeasonDropDownList.SelectedValue);
            this.RoundSqlDataSource.SelectParameters.Add("Language", LanguageHiddenField.Value);
                       
        }
        protected void RoundDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string CountryId = CountryIdHiddenField.Value;
            this.SqlDataSource1.SelectParameters.Clear();
            this.SqlDataSource1.SelectParameters.Add("Country", CountryId);
            this.SqlDataSource1.SelectParameters.Add("Season", this.SeasonDropDownList.SelectedValue);
            this.SqlDataSource1.SelectParameters.Add("Round", this.RoundDropDownList.SelectedValue);
            this.SqlDataSource1.SelectParameters.Add("Language", LanguageHiddenField.Value);
            this.SqlDataSource2.SelectParameters.Clear();
            this.SqlDataSource2.SelectParameters.Add("Country", CountryId);
            this.SqlDataSource2.SelectParameters.Add("Season", this.SeasonDropDownList.SelectedValue);
            this.SqlDataSource2.SelectParameters.Add("Round", this.RoundDropDownList.SelectedValue);
            this.SqlDataSource2.SelectParameters.Add("Language", LanguageHiddenField.Value);
            //this.CountryLabel.Text = ("Country: " + CountryId + "Season: " + this.SeasonDropDownList.SelectedValue + "Round: " + this.RoundDropDownList.SelectedValue);

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void AddToLive(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AddToLive")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                GND.ExecNivdalSQL("UserId", "MatchId", "Nivdal_LiveAddMatchToLive", ou.UserId, e.CommandArgument);
                Response.Redirect("Live.aspx");
            }
        }

        protected void AddSelectedToLive(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string MatchId;
            bool MatchAdded = false;
            foreach (GridViewRow r in GridView1.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        MatchId = (r.FindControl("MatchIdHiddenField") as HiddenField).Value;
                        if ((r.FindControl("LiveCheckBox") as CheckBox).Checked)
                        {
                            GND.ExecNivdalSQL("UserId", "MatchId", "Nivdal_LiveAddMatchToLive", ou.UserId, MatchId);
                            MatchAdded = true;
                            (r.FindControl("LiveCheckBox") as CheckBox).Checked = false;
                        }
                    }
                    catch { }
                }
            }
            if (MatchAdded)
            {
                try
                {
                    ((Master.Master.FindControl("LoginView1") as LoginView).FindControl("LiveListView") as Repeater).Visible = true;
                    ((Master.Master.FindControl("LoginView1") as LoginView).FindControl("LiveListView") as Repeater).DataBind();
                    ((Master.Master.FindControl("LoginView1") as LoginView).FindControl("LiveUpdatePanel") as UpdatePanel).Update();
                }
                catch { }
            }
        }
    }
}